package com.clinbrain;

import com.facebook.presto.spi.function.Description;
import com.facebook.presto.spi.function.ScalarFunction;
import com.facebook.presto.spi.function.SqlNullable;
import com.facebook.presto.spi.function.SqlType;
import com.facebook.presto.spi.type.StandardTypes;
import io.airlift.slice.Slice;
import io.airlift.slice.Slices;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 请求接口
 */
public final class AccessInterface {

    private AccessInterface() {

    }

    @ScalarFunction("access_interface")
    @Description("keyword_extract")
    @SqlNullable
    @SqlType("varchar")
    public static Slice accessInterface(@SqlType(StandardTypes.JSON) Slice dataSlice, @SqlType(StandardTypes.VARCHAR) Slice urlSli){

        if (null == urlSli || 0 == urlSli.length()) {
            throw new RuntimeException("The url cannot be null");
        }

        String url = urlSli.toStringUtf8();
        String line = dataSlice.toStringUtf8();

        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(line.getBytes());
        String result = null;
        try {
            result = HttpTool.doPostFormData(url, byteArrayInputStream);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return Slices.utf8Slice(result);
    }

    public static void main(String[] args) {

    }
}
